package com.boot.controller;


import com.boot.until.BaiduUntil;
import com.boot.until.HttpUtil;
import com.boot.until.UtilBase;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import org.jooq.DSLContext;
import org.jooq.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import static com.boot.until.HttpUtil.*;
import static com.boot.until.UtilBase.*;
import static com.boot.pojo.Tables.*;

/**
 * 公共处理类
 */
@RestController
@RequestMapping("query")
public class UntilController extends BaseController {

    @Autowired
    private DSLContext create;


    /**
     * 获取预加载的搜索词
     * @param query
     * @return
     */
    @RequestMapping(value = "get_baidu_result",method = RequestMethod.POST,consumes = "application/json",produces ="application/json;charset=utf-8")
    public String getBaiDu(@RequestBody String query ){
        //获取原生数据
        JsonObject json=new JsonParser ().parse ( query ).getAsJsonObject ();
        String param=getJsonAsString ( json,"param" );
        JsonArray jsonArray=getBaidu(param);
        //修改数据类型
        List params=new ArrayList (  );
        for (int i = 0; i < jsonArray.size (); i++) {
            JsonObject jsonObject=new JsonObject ();
            jsonObject.addProperty ( "value", jsonArray.get ( i ).getAsString ());
            params.add ( jsonObject );
        }
        return success (params);
    }


    /**
     * 获取新闻的类别
     * @return
     */
    @RequestMapping(value = "get_new_category",method = RequestMethod.POST,consumes = "application/json",produces ="application/json;charset=utf-8")
    public String getNewCategory(HttpServletResponse response,HttpServletRequest request){
     /*   //向header中取值打印
        String cookie=request.getHeader ( "setcookie" );
        log.info ( "【cookie的值】={}",cookie );*/
        JsonObject result=getCategory();
        return success (result);
    }

    /**
     * 获取新闻类别下的详情
     * @return
     */
    @RequestMapping(value = "get_new_detail",method = RequestMethod.POST,consumes = "application/json",produces ="application/json;charset=utf-8")
    public String getNewDatail(@RequestBody String query){
        JsonObject json=new JsonParser ().parse ( query ).getAsJsonObject ();
        String channel=getJsonAsString ( json,"channel" );
        Integer page=getJsonAsInt ( json,"page" );
        Integer pagenum=getJsonAsInt ( json,"pagenum" );
        //获取数据
        JsonObject result=getNewDetail ( channel,page,pagenum);
        return success (result);
    }


    /**
     * 查询培训公司
     * @return
     */
    @RequestMapping(value = "get_train",method = RequestMethod.POST,consumes = "application/json",produces ="application/json;charset=utf-8")
    public String getTrain(@RequestBody String query){
        try{
            //获取参数
            JsonObject json=new JsonParser ().parse ( query ).getAsJsonObject ();
            String key_word=getJsonAsString ( json,"key_word" );
            Integer type=getJsonAsInt ( json,"type" );
            Integer page=getJsonAsInt ( json,"page" );
            Integer pagenum=getJsonAsInt ( json,"pagenum" );

            Result result=create.select (  )
                    .from ( HDJ_TRAIN_NAME )
                    .where ( HDJ_TRAIN_NAME.NAME.like ( "%"+key_word+"%" ) )
                    .and ( HDJ_TRAIN_NAME.TYPE.eq ( type ) )
                    .limit ( pagenum )
                    .offset ( (page-1)*pagenum )
                    .fetch ();
            //总数
            Integer count=create.selectCount ()
                    .from ( HDJ_TRAIN_NAME )
                    .where ( HDJ_TRAIN_NAME.NAME.like ( "%"+key_word+"%" ) )
                    .and ( HDJ_TRAIN_NAME.TYPE.eq ( type ) )
                    .fetchOne ()
                    .into ( Integer.class );
            JsonArray array=dataChangeJsonArray ( result );
            JsonObject object=new JsonObject ();
            object.addProperty ( "count",count );
            object.add ( "rows",array );
            return success (object);
        }catch (Exception e){
            return error ();
        }
    }


    /**
     * 天气预报
     * @return
     */
    @RequestMapping(value = "get_wether",method = RequestMethod.POST,consumes = "application/json",produces ="application/json;charset=utf-8")
    public String getwether(HttpServletRequest request){
        JsonObject result= getWeather ( UtilBase.getIpAddr (request));
        return success (result);
    }

    /**
     * 获取所有的省市
     * @return
     */
    @RequestMapping(value = "get_province",method = RequestMethod.POST,consumes = "application/json",produces ="application/json;charset=utf-8")
    public String getProvince(){
        Result result=create.select (  )
                .from ( HDJ_PROVINCES )
                .orderBy ( HDJ_PROVINCES.ID.asc ())
                .fetch ();
        JsonArray array=dataChangeJsonArray ( result );
        return success (array);
    }

    /**
     * 获取所有的省市
     * @return
     */
    @RequestMapping(value = "get_cities/{provinceId}",method = RequestMethod.POST,consumes = "application/json",produces ="application/json;charset=utf-8")
    public String getCities(@PathVariable Integer provinceId){
        log.info ( "id={}",provinceId );
        Result result=create.select (  )
                .from ( HDJ_CITIES )
                .where ( HDJ_CITIES.PROVINCEID.eq ( provinceId.toString () ))
                .orderBy ( HDJ_CITIES.ID.asc ())
                .fetch ();
        JsonArray array=dataChangeJsonArray ( result );
        return success (array);
    }

    /**
     * 获取所有的区
     * @return
     */
    @RequestMapping(value = "get_areas/{cityId}",method = RequestMethod.POST,consumes = "application/json",produces ="application/json;charset=utf-8")
    public String getAreas(@PathVariable Integer cityId){
        Result result=create.select (  )
                .from ( HDJ_AREAS )
                .where ( HDJ_AREAS.CITYID.eq ( cityId.toString () ))
                .orderBy ( HDJ_AREAS.ID.asc ())
                .fetch ();
        JsonArray array=dataChangeJsonArray ( result );
        return success (array);
    }


    /**
     * 根据地区获得天气信息
     * @param areaName
     * @return
     */
    @RequestMapping(value = "get_weather_area",method = RequestMethod.POST,produces ="application/json;charset=utf-8")
    public String getWeatherByarea( String areaName){
        JsonObject result= HttpUtil.getWeatherByarea(areaName);
     return success (result);
    }

    /**
     * 百度的音频合成
     */
    @RequestMapping(value = "get_baidu_audio",method = RequestMethod.GET)
    public void getBaiduAudio(String src,HttpServletResponse response){
        byte[] audio= BaiduUntil.getBaoduAudio ( src );
        try {
            response.getOutputStream ().write ( audio );
        } catch (IOException e) {
            e.printStackTrace ();
        }
    }

    /**
     * 图灵机器人的输出
     * @param info
     */
    @RequestMapping(value = "get_robot",method = RequestMethod.POST,produces ="application/json;charset=utf-8")
    public String getTuLingRobot(String info){
        JsonObject result= HttpUtil.getRobot ( info.trim ()  );
      return success (result);
    }

}
